import React from 'react';

import { useRequest } from '@/hooks';
import { LeaveList } from '.';
import styles from '../index.less';
import { queryLeaveList } from '../../service';

export default function CurrentLeaveList(location) {
    const { query } = location.location;
    const [condition, setCondition] = React.useState({
        pageNo: 1,
        pageSize: 10,
        keyword: '',
        companyCode: '',
        organizationCode: '',
        dutyCode: '',
        currentDealEmployeeName: '',
        // dimissionType: 1,
        rankValues: [],
        ...query,
        dimissionStatus: query.dimissionStatus ? query.dimissionStatus.split(',') : []
    });

    // 查询条件
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const queryCondition = React.useMemo(
        () =>
            Object.keys(condition).reduce((ret, key) => {
                const item = condition[key];

                if (!item && item !== 0) {
                    return ret;
                }
                if (Array.isArray(item) && item.length === 0) {
                    return ret;
                }
                return { ...ret, [key]: item };
            }, {}),
        [JSON.stringify(condition)]
    );

    const sortOrders = React.useMemo(() => {
        if (!condition?.sort) {
            return {};
        }
        return condition?.sort.split(',').reduce((pre, item) => {
            const [key, value] = item.split(' ');
            return { ...pre, [key]: `${value}end` };
        }, {});
    }, [condition?.sort]);
    // 查询
    const { data, loading, run } = useRequest(() => queryLeaveList(queryCondition), {
        refreshDeps: [queryCondition]
    });

    return (
        <div className={styles.current}>
            <LeaveList
                type={1}
                data={data}
                loading={loading}
                setCondition={setCondition}
                sortOrders={sortOrders}
                condition={condition}
                fetchList={run}
                location={location}
            />
        </div>
    );
}
